package num441;

/**
 * @author : DuJiabao
 * @Project : sword-finger-offer
 * @Time : 2021/5/8 20:51
 * @Desc :
 */
public class Solution {
  public int arrangeCoins(int n) {
    int left = 0;
    int right = n;
    int temp = 0;
    while (left <= right) {
      int mid = (left + right) / 2;
      double express = 2.0 * n / (mid + 1);  // 防止int溢出
      if (mid == express) {
        return mid;
      } else if (mid > express) {
        right = mid - 1;
      } else {
        temp = mid;
        left = mid + 1;
      }
    }
    return temp;
  }

  public static void main(String[] args) {
    int res = new Solution().arrangeCoins(5);
    System.out.println(res);
  }
}
